﻿using AngryManagementMayhem.Content.Textures.MainMenu;
using AngryManagementMayhem.Controller;
using AngryManagementMayhem.GameCode.Screens.MainMenuScreen.MainMenuObjects;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.GameObjects.Primitives;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Input;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace AngryManagementMayhem.GameCode.Screens.MainMenuScreen
{
    public class CreateGameScreen : Screen
    {
        private Textbox _gameName;

        public override void LoadContent()
        {
            PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X, 0);
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(0, 0), 1500, false, 0));

            AddChildren(new MenuBackground("Create a custom game"));

            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Step 1 of 2")
            {
                PositionGridBound = new TE_Vector2(42.5f, 2),
                TextAlign = TE_Rectangle.Alignment.CenterRight
            });

            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Enter the name of the game")
            {
                PositionGridBound = new TE_Vector2(2.5f, 5),
                TextAlign = TE_Rectangle.Alignment.TopLeft
            });

            AddChildren(new RectanglePrimitive
            {
                Shape = new TE_Rectangle(2, 6.5f, 40, 4.5f),
                FillColor = Color.Transparent,
                OutlineColor = Color.White,
                OutlineThickness = 2
            });
            _gameName = new Textbox(MainMenuSpriteSheet.GetSprite, GameController.GameName, 25)
                {
                    PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X/2, 9.25f),
                    Scale = new TE_Vector2(3, 3),
                    TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                    Filter = Textbox.TextboxFilter.Alphanumeric
                };

            var nextButton = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.Next) })
                {
                    PositionGridBound = new TE_Vector2(40.5f, 26.5f)
                };
            nextButton.OnClick += i => SaveMap();

            var previousButton = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.Next), Mirror = ImageContainer.MirrorState.MirrorHorizontal })
            {
                PositionGridBound = new TE_Vector2(38, 26.5f)
            };
            previousButton.OnClick += i => ChangeScreen("gameLobby");

            AddChildren(_gameName, nextButton, previousButton);

            KeyboardHandler.OnKeyboardPress += OnKeyPress;

            base.LoadContent();
        }

        private void OnKeyPress(KeyboardInput input)
        {
            if(input.KeyCode == KeyboardInput.Key.Enter)
                SaveMap();
        }

        void SaveMap()
        {
            if (_gameName.Text[0].Length == 0)
                return;
            KeyboardHandler.OnKeyboardPress -= OnKeyPress;
            GameController.GameName = _gameName.Text[0];
            ChangeScreen("createGame.Mode");
        }

        public override void Die(double timeToLive)
        {
            ClearAnimation();
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(-Game.Options.ScreenSize.X, 0), timeToLive, false, 0));

            base.Die(timeToLive);
        }
    }
}